/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.tableditor;

import javax.swing.*;

import jdbcmanager.dbtools.datatypes.*;

/**
 * 
 * @author Francisco Morero Peyrona
 */
final class JxTableRelations extends JxTableParent
{
   private final static int COL_PRIMARY_KEY = 0;
   private final static int COL_FOREIGN_KEY = 1;
   
   private JComboBox cmbPrimaryKey;     // ComboBox with all primary table columns name 
   private JComboBox cmbForeignKey;     // ComboBox with all foreign table columns name
   
   //-------------------------------------------------------------------------//
   
   JxTableRelations()
   {    
      setModel( new RelationTableModel() );
      setHead( new String[] { "Primary Column", "Foreign Column" } );
      setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
      init();
   }
   
   /**
    * @see jdbcmanager.dbtools.tableditor.JxTableParent#createEmptyRow()
    */
   protected Object createEmptyRow() 
   {
      return new RelationColumnDefinition();
   }
   
   //-------------------------------------------------------------------------//
   
   void setColumns( RelationColumnDefinition[] aData )
   {      
      setData( aData );
   }
   
   RelationColumnDefinition[] getColumns()
   {
      RelationColumnDefinition[] aoCol = new RelationColumnDefinition[ this.vData.size() ];
      
      this.vData.copyInto( aoCol );
      
      return aoCol;
   }
   
   void setAvailableColumNames4PrimaryKey( String[] asColName )
   {
      // Change combo contents
      this.cmbPrimaryKey.removeAllItems();
      
      for( int n = 0; n < asColName.length; n++ )
         this.cmbPrimaryKey.addItem( asColName[n] );
      
      // If previously selected item (primary columns name) does not exists anymore: set it to null
      RelationColumnDefinition[] aRCD = getColumns();
      
      for( int n = 0; n < aRCD.length; n++ ) 
      {
         String sColumn = aRCD[n].getPrimaryColumn();
         int    nIndex  = ((DefaultComboBoxModel) this.cmbPrimaryKey.getModel()).getIndexOf( sColumn );
         
         if( nIndex < 0 )
            aRCD[n].setPrimaryColumn( null );
      }
   }
   
   void setAvailableColumNames4ForeignKey( String[] asColName )
   {
      // Change combo contents
      this.cmbForeignKey.removeAllItems();
      
      for( int n = 0; n < asColName.length; n++ )
         this.cmbForeignKey.addItem( asColName[n] );
      
      // If previously selected item (foreign columns name) does not exists anymore: set it to null
      RelationColumnDefinition[] aRCD = getColumns();
        
      for( int n = 0; n < aRCD.length; n++ ) 
      {
         String sColumn = aRCD[n].getForeignColumn();
         int    nIndex  = ((DefaultComboBoxModel) this.cmbForeignKey.getModel()).getIndexOf( sColumn );
         
         if( nIndex < 0 )
            aRCD[n].setForeignColumn( null );
      }
   }
   
   //-------------------------------------------------------------------------//
   
   private void init()
   {
      this.cmbPrimaryKey = new JComboBox();
      (getColumnModel().getColumn( COL_PRIMARY_KEY )).setCellEditor( new DefaultCellEditor( this.cmbPrimaryKey ) );
      
      this.cmbForeignKey = new JComboBox();
      (getColumnModel().getColumn( COL_FOREIGN_KEY )).setCellEditor( new DefaultCellEditor( this.cmbForeignKey ) );
   }
   
   //---------------------------------------------------------------//
   // INNER CLASS: TABLE MODEL
   //---------------------------------------------------------------//
   protected class RelationTableModel extends JxTableParent.ParentTableModel
   {
      public Object getValueAt( int nRow, int nCol )
      {
         RelationColumnDefinition column = (RelationColumnDefinition) JxTableRelations.this.vData.get( nRow );
         Object  oValue;

         switch( nCol )
         {
            case COL_PRIMARY_KEY: oValue = column.getPrimaryColumn();  break;
            case COL_FOREIGN_KEY: oValue = column.getForeignColumn();  break;
            default: oValue = "<Error>";  // Avoid to create this String instance if there is no error
         }
         
         return oValue;
      }

      public void setValueAt( Object oValue, int nRow, int nCol )
      {
         RelationColumnDefinition column = (RelationColumnDefinition) JxTableRelations.this.vData.get( nRow );

         switch( nCol )
         {
            case COL_PRIMARY_KEY: column.setPrimaryColumn( (String) oValue );  break;
            case COL_FOREIGN_KEY: column.setForeignColumn( (String) oValue );  break;
         }
      }
   } // End of table model 
}